package com.tcm.utils;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tcm.config.AppProperties;
import com.tcm.entity.Book;
import com.tcm.entity.dto.ItemCorrectDto;
import com.tcm.entity.vo.ItemCorrectResp;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 题目工具类
 * @author Guqier
 * @version 1.0
 * @date 2023/7/12 21:00
 */
public class ItemUtil {


    /**
     * 解析题目获取答案
     * @param itemContent 题目内容
     * @return 答案列表
     */
    public static List<String> analyzeClozeItemToAnswer(String itemContent){
        // 使用正则表达式提取题目中的答案
        Pattern pattern = Pattern.compile("\\{([^}]+)}");
        Matcher matcher = pattern.matcher(itemContent);
        // 遍历结果集
        List<String> ans = new ArrayList<>();
        while (matcher.find()) {
            String substring = matcher.group(1);
            ans.add(substring);
        }
        return ans;
    }

    /**
     * 解析题目获取标记后的题目
     * @param itemContent 题目内容
     * @return 标记后的题目
     */
    public static String analyzeClozeItemToMarkContent(String itemContent){
        // 使用正则表达式提取题目中的答案
        Pattern pattern = Pattern.compile("\\{([^}]+)}");
        Matcher matcher = pattern.matcher(itemContent);
        // 遍历结果集
        StringBuffer result = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(result, "_");// 将空格处替换为@@
        }
        matcher.appendTail(result);
        return result.toString();
    }

    /**
     * 递归查找当前节点父节点ID并返回父节点ID列表
     * @param currId 当前节点ID
     * @param books 书籍列表
     * @param parentIds 父节点ID列表
     */
    public static void getBookParentIdByRecursion(Long currId, List<Book> books, List<Long> parentIds){
        for (Book book : books) {
            if (book.getId().equals(currId)){
                parentIds.add(book.getId());
                if (book.getParentId() != null){
                    getBookParentIdByRecursion(book.getParentId(), books, parentIds);
                }
                break;
            }
        }
    }

    /**
     * 获取Json格式的用户答案
     * @return Json格式的用户答案
     */
    public static String getUserItemJsonAns(ItemCorrectResp resp) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        if (resp.getType().equals(AppProperties.ITEM_TYPE_SUBJECT)){
            List<ItemCorrectDto.Question> questions = resp.getQuestions().stream().filter(question -> StringUtils.hasText(question.getAns())).map(question -> {
                ItemCorrectDto.Question q = new ItemCorrectDto.Question();
                BeanUtils.copyProperties(question, q);
                return q;
            }).collect(Collectors.toList());
            return objectMapper.writeValueAsString(questions);
        }else {
            return objectMapper.writeValueAsString(resp.getAns());
        }
    }

}
